<template>
  <div class="box">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>商品添加</span>
      </div>
      <div>
        <el-form ref="form" :model="form" label-width="80px" :rules="rules">
          <el-form-item label="商品名称" prop="name">
            <el-input v-model="form.name" clearable></el-input>
          </el-form-item>
          <el-form-item label="商品分类" prop="category">
            <el-select v-model="form.category" placeholder="请选择商品分类">
              <el-option
                :label="item"
                :value="item"
                v-for="item in cateList"
                :key="item"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="商品价格" prop="price">
            <el-input-number
              v-model="form.price"
              @change="handleChange"
              :min="1"
              label="请输入价格"
            ></el-input-number>
          </el-form-item>

          <el-form-item label="商品图片" prop="imgUrl">
            <el-upload
              class="avatar-uploader"
              :action="action"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="form.imgUrl" :src="form.imgUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>

          <el-form-item label="商品描述">
            <el-input type="textarea" v-model="form.goodsDesc"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">添加商品</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
import { goodsCategories, goodsAdd } from "../../api/goods";
export default {
  data() {
    return {
      action: "http://127.0.0.1:5000/goods/goods_img_upload",
      cateList: [],
      form: {
        name: "",
        category: "",
        price: "",
        goodsDesc: "",
        imgUrl: "",
      },
      rules: {
        name: [{ required: true, message: "请输入商品名称", trigger: "blur" }],
        category: [
          { required: true, message: "请选择商品分类", trigger: "blur" },
        ],
        price: [{ required: true, message: "请输入商品价格", trigger: "blur" }],
        goodsDesc: [
          { required: true, message: "请输入商品描述", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    //获取启用的分类名
    async startCate() {
      this.cateList = await goodsCategories({ params: {} });
      // console.log(res);
    },

    //添加商品
   async onSubmit() {
      let res =await goodsAdd(this.form)
      console.log(res);
      let {code,msg}=res

      if (code===0) {
        this.$message({
              message: msg,
              type: "success",
            });
            this.$router.push("/goods/goods-list")
      }else{
        this.$message.error(msg);
      }
    },
    //成功上传
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      //将图片回填
      this.form.imgUrl = res.imgUrl;
    },
    //成功上传前
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg" || file.type === "image/gif";
      const isLt2M = file.size / 1024 / 1024 <= 10;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 0MB!");
      }
      return isJPG && isLt2M;
    },

    //价格增减
    handleChange(value) {
      //console.log(value);
    },
  },

  created() {
    this.startCate();
  },
};
</script>

<style lang="less" scoped>
.avatar-uploader .el-upload {
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.el-icon-plus {
  border: 1px dashed #b1b0b0;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>